from pynput import keyboard
from pynput import mouse
import time

alt_pressed = False
altr_pressed = False
shift_pressed = False
ctrl_pressed = False
ctrlr_pressed = False
insert_pressed = False
ctrlr_pressed_time = 0
defer_task = None

def modikey_check_p(key):
    global alt_pressed, shift_pressed, ctrl_pressed, ctrlr_pressed, altr_pressed, insert_pressed, ctrlr_pressed_time, defer_task
    
    if  'insert'  in str(key):
        insert_pressed = True
        
    if 'alt' in str(key):
        if 'alt_r' in  str(key) or 'alt_gr' in str(key):
            altr_pressed = True
        else:
            alt_pressed = True
    
    if 'shift' in str(key):
        shift_pressed = True
        
    if 'ctrl' in str(key):
        if 'ctrl_r' in str(key):
            ctrlr_pressed = True
            if time.time() -ctrlr_pressed_time <0.2:
                defer_task = 5
                #mouse.Controller().click(mouse.Button.left,2)                        
            ctrlr_pressed_time = time.time()
            
        else:
            ctrl_pressed = True

def modikey_check_r(key):
    global alt_pressed, shift_pressed, ctrl_pressed, ctrlr_pressed, altr_pressed, insert_pressed
    if  'insert'  in str(key):
        insert_pressed = False
        
    if 'ctrl' in str(key):
        if 'ctrl_r' in str(key):
            ctrlr_pressed = False
        else:
            ctrl_pressed = False
        
    if 'shift' in str(key):
        shift_pressed = False
        
    if 'alt' in str(key):
        if 'alt_r' in  str(key) or 'alt_gr' in str(key):
            altr_pressed = False
        else:
            alt_pressed = False


how_many_inserts_before_release = 0
effect_insert_co = 0
skip = False
left_pressed = False
is_working = True




def has_modify():
    return alt_pressed or shift_pressed or ctrl_pressed or ctrlr_pressed or altr_pressed

def no_key_pressed():
    if not has_modify() and not insert_pressed:
        return True
    return False


def on_press(key):
    global how_many_inserts_before_release, effect_insert_co, skip, left_pressed, defer_task, is_working    
    print(time.time(), 'press', key, how_many_inserts_before_release, effect_insert_co)
    modikey_check_p(key)
    
    
    if left_pressed and  'ctrl_r' in str(key):
        defer_task = 3
        #mouse.Controller().release(mouse.Button.left)
        left_pressed = False
        return
           
    if  'insert' not in str(key):
        return
    
    if not has_modify():
        effect_insert_co += 1
        if effect_insert_co == 100:
            effect_insert_co = 0
    
    how_many_inserts_before_release += 1
    if how_many_inserts_before_release > 1:
        return
    
    if skip:
        skip = False
        return
    
    if not has_modify():
        defer_task = 4
        #mouse.Controller().click(mouse.Button.left,1)        
        print('left click', time.time())
        left_pressed = False        
        return
    
    if ctrlr_pressed or altr_pressed:
        if not left_pressed:
            defer_task = 1
            #mouse.Controller().press(mouse.Button.left)
            left_pressed = True
        else:
            defer_task = 3
            #mouse.Controller().release(mouse.Button.left)
            left_pressed = False
        return
    
    if ctrl_pressed:
        defer_task = 2
        #mouse.Controller().click(mouse.Button.right,1)
        left_pressed = False
        
    if alt_pressed:
        is_working = not is_working
        effect_insert_co = 0
        
        
                    
        
def on_release(key):
    print(time.time(), 'release', key)
    modikey_check_r(key)
    global skip, how_many_inserts_before_release, defer_task
    how_many_inserts_before_release = 0
    if not is_working :
        return
    
    if defer_task and no_key_pressed() and effect_insert_co % 2 == 0:
        if defer_task == 4:
            mouse.Controller().click(mouse.Button.left,1)
        if defer_task == 5:
            mouse.Controller().click(mouse.Button.left,2)            
        if defer_task == 1:
            mouse.Controller().press(mouse.Button.left)
        if defer_task == 2:
            mouse.Controller().click(mouse.Button.right,1)
        if defer_task == 3:
            mouse.Controller().release(mouse.Button.left)
            
            
        defer_task = None
        
    if effect_insert_co % 2 != 0:
        skip = True
        keyboard.Controller().press(key)
        keyboard.Controller().release(key)
        
        
listener = keyboard.Listener(
    on_release=on_release,
    on_press=on_press,
    
)
listener.start()
import time
while True:
    time.sleep(100)